package com.wenhai.leetcode.array;

import java.util.Arrays;

/**
 * https://leetcode-cn.com/problems/sort-colors/
 *
 * @author wenhai
 * @date 2021/3/1
 */
public class _75颜色分类 {

    public void sortColors(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }
        int zero = -1;
        int i    = 0;
        int two  = nums.length;
        while (i < two) {
            if (nums[i] == 0) {
                zero++;
                swap(nums, zero, i);
                i++;
            } else if (nums[i] == 2) {
                two--;
                swap(nums, two, i);
            } else {
                i++;
            }
        }

        System.gc();
    }



    private void swap(int[] nums, int descIndex, int targetIndex) {
        int temp = nums[descIndex];
        nums[descIndex] = nums[targetIndex];
        nums[targetIndex] = temp;
    }

    public static void main(String[] args) {
        int[] nums = {2, 0, 2, 1, 1, 0};
        new _75颜色分类().sortColors(nums);
        System.out.println(Arrays.toString(nums));

    }
}
